package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbBrandMapper;
import com.guigu.qingcheng.pojo.TbBrand;
import com.guigu.qingcheng.service.TbBrandService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
*
* @author Jiang
* @date 2020/07/25
*/
@Service
public class TbBrandServiceImpl implements TbBrandService {
    @Autowired(required = false)
    private TbBrandMapper tbBrandMapper;



    @Override
    public List<TbBrand> queryAll() {
        List<TbBrand> tbBrand = tbBrandMapper.selectAll();
            return tbBrand;
    }

    @Override
    public PageUtil<TbBrand> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbBrand> pages = (Page<TbBrand>) tbBrandMapper.selectAll();
        PageUtil<TbBrand> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbBrand> pageQueryByAll(Integer pageNum, Integer pageSize, TbBrand tbBrand) {

        Example example=new Example(TbBrand.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        if(!StringUtils.isEmpty(tbBrand.getId())){
            criteria.andEqualTo("id",tbBrand.getId());
        }
        if(!StringUtils.isEmpty(tbBrand.getName())){
           criteria.andLike("name",tbBrand.getName());
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbBrand> pages = (Page<TbBrand>) tbBrandMapper.selectByExample(example);
        PageUtil<TbBrand> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbBrand(TbBrand tbBrand) {
        int insert = tbBrandMapper.insert(tbBrand);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbBrand(TbBrand tbBrand) {
        int i = tbBrandMapper.updateByPrimaryKeySelective(tbBrand);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbBrandMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbBrand findById(Integer id) {
        TbBrand tbBrand = tbBrandMapper.selectByPrimaryKey(id);
        return tbBrand;
    }

    @Override
    public List<TbBrand> queryTbBrandByCategoryId(Integer id) {
        List<TbBrand> tbBrands = tbBrandMapper.queryTbBrandByCategoryId(id);
        if (tbBrands.size() <= 0){
            tbBrands = tbBrandMapper.selectAll();
        }
        return tbBrands;
    }

    @Override
    public List<Map> queryByCategoryName(String name) {
        return tbBrandMapper.findByCategoryName(name);
    }
}
